<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;


class User extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($page = 1)
    {
        $count = Db::name('user')->count('id');
        $count = ceil($count/10);
        //判断1<=$page<max
        if ($page < 1){
            $page = 1;
        }elseif ($page > $count){
            $page = $count;
        }
        $data = Db::name('user')->field('password',true)->order(['id' => 'aec'])->page($page,10)->select();
        return view('user/index',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'admin_name'=>Session::get('admin_name'),
            'count'=>$count,
            'page'=>$page,
            'data' => $data,
            'admin_id'=>Session::get('admin_id')
        ]);
    }

    public function search($page = 1,Request $request)
    {
        $post = $request->post();
        $count = Db::name('user')->where('tel','like','%'.$post['like'].'%')->count('id');
        $count = ceil($count/10);
        //判断1<=$page<max
        if ($page < 1){
            $page = 1;
        }elseif ($page > $count){
            $page = $count;
        }
        $data = Db::name('user')->field('password',true)->order(['id' => 'aec'])->where('tel','like','%'.$post['like'].'%')->page($page,10)->select();
        return view('user/index',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'admin_name'=>Session::get('admin_name'),
            'count'=>$count,
            'page'=>$page,
            'data' => $data,
            'admin_id'=>Session::get('admin_id')
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
   public function create()
   {
       $data = Db::name('role')->select();
        return view('user/create',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data
        ]);
   }

   public function doCreate(Request $request)
   {    //向user表添加post数据,向user_info表添加新增时间,向ui_id表添加user表和info表的关联.
        Db::startTrans();
        $post = $request->post();
        $name = $post['name'];
        $pass = $post['pass'];
        $tel = $post['tel'];
        $status = key_exists('status',$post)?'1':'0';
        $arr = ['name'=>$name, 'password'=>$pass, 'tel'=>$tel, 'status'=>$status];
        $sql1 = DB::name('user')->insertGetId($arr);
        $sql2 = DB::name('user_info')->insertGetId(['addtime'=>time()]);
        $sql3 = DB::name('ui_id')->insert(['user_id'=>$sql1, 'user_info_id'=>$sql2]);
        Db::commit();
        if($sql3){
            return 1;
        }else{
            Db::rollback();
            return 0;
        }
        
   }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $post = $request->post();
        //处理数据
        if(key_exists('status',$post)){
            $status = '1';
        }else{
            $status = '0';
        }
        $data = [
            'name' => $post['name'],
            'password' => md5($post['pass']),
            'tel' => $post['tel'],
            'status' => $status
        ];
//        var_dump($data);die;
        //执行数据库操作
        $result = Db::name('user')->insert($data);
        $userId = Db::name('user')->getLastInsID();
        $data = DB::name('user_info')->insert(['addtime'=>time()]);
        $dataId = Db::name('user_info')->getLastInsID();
        $uiid = ['user_id'=>$userId, 'user_info_id'=>$dataId];
        $value = DB::name('ui_id')->insert($uiid);
        if ($result && $data && $value){
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //判断是否是ajax请求
        if (!Request::instance()->isAjax()){
            $this->error('你走错地方了,亲爱的');
        }
        $data = Db::name('user')->field('pass',true)->find($id);
        if ($data){
            $data['status'] = 1;
        }else{
            $data['info'] = '没有这个用户';
        }
        return $data;
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $data = Db::name('ui_id')
            -> alias('uii')
            -> where('uii.user_id ='.$id)
            -> join('gqx_user u','u.id = uii.user_id')
            -> join('gqx_user_info ui','ui.id = uii.user_info_id')
            -> select();
        array_push($data,['id'=>$id]);
//        var_dump($data[1]['id']);
        return view('user/edit',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'row' => $data[0],
            'admin_name'=>Session::get('admin_name'),
            'id'=>$data[1]['id']
        ]);
    }

    public function doEdit($id)
    {
        // 获取所有数据
        $data = $_POST;
        var_dump($data);
        // 获取status值
        $status = (key_exists('status',$_POST))?'1':'0';
        $data = array_merge($data,['status'=>$status]);
        // 切割出user表的值和user_info表的值
        $user = ['name'=>$data['name'], 'password'=>$data['pass'], 'tel'=>$data['tel'], 'status'=>$data['status']];
        foreach ($user as $k => $v) {
            if($v==''){
                unset($user[$k]);
            }
        }
        $info = ['age'=>$data['Age'], 'birthday'=>$data['birthday'], 'province'=>$data['province'], 'integral'=>$data['integral'], 'email'=>$data['email'], 'addtime'=>$data['addtime'], 'experience' => $data['experience'], 'sex'=>$data['sex'], 'is_marry'=>$data['marry']];
        foreach ($info as $k => $v) {
            if($v==''){
                unset($info[$k]);
            }
        }
        var_dump($info);
        $datai = DB::name('user_info')->where('id='.$id)->update($info);
        $datau = DB::name('user')->where('id='.$id)->update($user);
        var_dump($datai);
        var_dump($datau);
        die;
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        // 判断是否有删除权限
       //执行数据库删除操作
        $data = Db::name('user')->delete($id);
        /*$uiid = DB::name('ui_id')->field('user_info_id')->where('user_id='.$id)->select();
        $dataui = DB::name('ui_id')->where('user_id='.$id)->delete();
        $datai = DB::name('user_info')->where('id='.$uiid[0]['user_info_id']);*/
        if ($data > 0){
            $result['status'] = 1;
            $result['info'] = '删除成功';
        }else{
            $result['info'] = '删除失败';
        }
        return $result;
    }

    public function show($id)
    {
        $data = Db::name('ui_id')
            -> alias('uii')
            -> where('uii.user_id ='.$id)
            -> join('gqx_user u','u.id = uii.user_id')
            -> join('gqx_user_info ui','ui.id = uii.user_info_id')
            -> select();
//        var_dump($data);die;
        return view('user/show',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'admin_name'=>Session::get('admin_name'),
            'row' => $data[0],
        ]);
    }

    public function editStatus($id)
    {
        $status = DB::name('user')->where(['id'=>$id])->field('status')->find();
        $status = $status['status']?'0':'1';
        $data = DB::name('user')->where(['id'=>$id])->update(['status' => $status]);
        if($data){
            return $data;
        }
    }
}
